package com.filldream.fastboot.web.controller.common;

import com.filldream.fastboot.base.entity.OssResource;
import com.filldream.fastboot.base.request.common.FilePathSTO;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.exception.GlobalException;
import com.filldream.fastboot.common.util.Assert;
import com.filldream.fastboot.core.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping(value = "/common/upload")
@Api(tags = "文件上传管理")
@Slf4j
//@PassTokenAnnotation
public class UploadController {

    @Resource
    private UploadService uploadService;

    //---------------------阿里云OSS------------------------

    @PostMapping(value = "/uploadOssFile")
    @ApiOperation(value = "阿里OSS上传文件",notes = "fileName为空则随机文件名")
    public R<List<OssResource>> uploadFile(@RequestParam(value = "file", required = false) MultipartFile file, String filePrefix) {
        return R.successd(uploadService.uploadOssFile(file,filePrefix));
    }

    @PostMapping(value = "/batchUploadOss")
    @ApiOperation(value = "阿里OSS批量上传文件",notes = "批量上传文件")
    public R<List<OssResource>> batchUpload(@RequestParam(value = "file", required = false) MultipartFile[] file) {
        Assert.checkBoolean( file.length <= 9 , ErrorCode.BASE_ERR.reMsg("文件数不得超过9个"));
        List<OssResource> resultList = new ArrayList<>();
        for (MultipartFile itemFile : file) {
            resultList.addAll(uploadService.uploadOssFile(itemFile,""));
        }
        return R.successd( resultList );
    }

    @PostMapping(value = {"/delOssFile"})
    @ApiOperation(value = "阿里OSS删除文件")
    public B delFile(@RequestBody @Valid FilePathSTO filePathSTO) {
        return uploadService.delOssFile(filePathSTO.getFilePath());
    }


    //---------------------本地文件------------------------

    @PostMapping(value = "/uploadFileToLocal")
    @ApiOperation(value = "上传文件到本地",notes = "上传文件到本地")
    public R<List<OssResource>> uploadFileToLocal(@RequestParam(value = "file", required = false) MultipartFile file) {
        return R.successd( uploadService.uploadFileToLocal(file) );
    }



    //---------------------腾讯COS------------------------

    @PostMapping(value = "/uploadOcsFile")
    @ApiOperation(value = "上传文件到腾讯")
    public R<List<OssResource>> txUploadFile(@RequestParam(value = "file", required = false) MultipartFile file, String filePrefix) {
        try {
            return R.successd( uploadService.uploadCosFile(file,filePrefix) );
        } catch (IOException e) {
            throw new GlobalException(ErrorCode.RESOURCE_UPLOAD_ERROR.reMsg(e.getMessage()));
        }
    }

    @PostMapping(value = "/delOcsFile")
    @ApiOperation(value = "删除腾讯文件")
    public B txDelFile(@RequestBody FilePathSTO filePathSTO) {
        return uploadService.delOcsFile(filePathSTO.getFilePath());
    }



}
